<?
class System{//Lop he thong,Cac ham dung chung thong dung cho vao day
    static $false = false;
    function send_mail($to,$subject,$content,$from = WEB_NAME,$cc=''){
        require_once(ROOT_PATH.'includes/mailer/class.phpmailer.php');
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SetLanguage("vn",ROOT_PATH.'includes/mailer/');
        $mail->Host    = SMTP_HOST;
        $mail->Port     = SMTP_PORT;
        $mail->SMTPAuth = SMTP_AUTH;
        $mail->Username = SMTP_USER;    // SMTP username
        $mail->Password = SMTP_PASS;                 // SMTP password
        $mail->From     = SMTP_FROM_EMAIL;        // Email duoc gui tu???
        $mail->FromName = WEB_NAME;                // Ten hom email duoc gui
        $mail->AddAddress($to,"");                    // Dia chi email va ten nhan
        if($cc){
            $mail->AddCC($cc,"");                       // Dia chi email va ten nhan
        }
       
        $mail->IsHTML(true);                        // Gui theo dang HTML
        $mail->Subject  =  "[" . WEB_NAME . "] ".$subject;                // Chu de email
        $mail->Body    =  $content;                // Noi dung html
        if(!$mail->Send()){return false;}else{return true;}
    }
   
    static function sendECEmail($to, $subject, $content,$FromNameMail = WEB_NAME)
    {
        //mail of QuangLX
        require_once(ROOT_PATH.'includes/mailer/class.phpmailer.php');
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SetLanguage("vn",ROOT_PATH.'includes/mailer/');
        $mail->Host    = SMTP_HOST;
        $mail->Port     = SMTP_PORT;
        $mail->SMTPAuth = SMTP_AUTH;
        $mail->Username = SMTP_USER;    	// SMTP username
        $mail->Password = SMTP_PASS;                 // SMTP password
        $mail->From     = SMTP_FROM_EMAIL;        // Email duoc gui tu???
        $mail->FromName = $FromNameMail;                // Ten hom email duoc gui
        $mail->AddAddress($to,"");                    // Dia chi email va ten nhan
       
        $mail->IsHTML(true);                        // Gui theo dang HTML
        $mail->Subject  =  "[" . WEB_NAME . "] ".$subject;                // Chu de email
        $mail->Body    =  $content;                // Noi dung html
        if(!$mail->Send()){return false;}else{return true;}
    }
   
    static function halt(){
        exit();
    }
   
    static function debug($array){
        echo '<div align="left"><pre>';
        print_r($array);
        echo '</pre></div>';
    }
    
    static function br($stt = 1){
        for ($i=1;$i<=$stt;$i++)
        {
 	       echo '<br />';
        }
    }

    static function getSystemMessage($type='message', $remove=true) {
        if(isset($_SESSION['_system']['_sys_msg'][$type]))
            $messages = $_SESSION['_system']['_sys_msg'][$type];
        else
            $messages = null;
        if($remove) unset($_SESSION['_system']['_sys_msg'][$type]);
        return $messages;
    }

    static function setSystemMessage($message, $type='message') {
        $_SESSION['_system']['_sys_msg'][$type][] = $message;
    }

    /**
     * thêm mới contact vào hệ thống mail của sting
     * @static
     * @param $email
     * @param int $mtype
     * @param string $fullname
     * @param int $mobile
     * @return bool|mixed
     * example: ('tlkit158@gmail.com',1,'thinh le khac')
     */

    static function addContactToSting($email, $mtype =1, $fullname='', $mobile=0){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key'].$email);
        $post_fields['email'] = $email;
        $post_fields['domain'] = CGlobal::$mail_sting_server['domain'];
        $post_fields['mtype'] = $mtype;
        $post_fields['fullname'] = $fullname;
        $post_fields['mobile'] = $mobile;
        //execute the API Call
        $result = $client->__call('addContact',$post_fields);
        $result = json_decode($result);
        if($result->status == 1){
            return true;
        }else{
            return $result;
        }

    }

    /**
     * xoá một contact từ hệ thống mail của sting
     * @static
     * @param $email
     * @param $mtype
     * @return bool|mixed
     */

    static function removeContactFromSting($email,$mtype){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key'].$email);
        $post_fields['email'] = $email;
        $post_fields['domain'] = CGlobal::$mail_sting_server['domain'];
        $post_fields['mtype'] = $mtype;
        //execute the API Call
        $result = $client->__call('removeContact',$post_fields);
        $result = json_decode($result);
        if($result->status == 1){
            return true;
        }else{
            return $result;
        }
    }

    /**
     * xoá danh sách email theo mtype của sting
     * @static
     * @param $mtype
     * @return bool|mixed
     */
    static function removeListContactFromSting($mtype){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key']);
        $post_fields['domain'] = CGlobal::$mail_sting_server['domain'];
        $post_fields['mtype'] = $mtype;
        //execute the API Call
        $result = $client->__call('remove_list',$post_fields);
        $result = json_decode($result);
        if($result->status == 1){
            return true;
        }else{
            return $result;
        }
    }

    /**
     * lấy tổng số các email trên hệ thống mail của sting
     * @static
     * @return mixed
     */
    static function getListContact(){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key']);
        $post_fields['domain'] = CGlobal::$mail_sting_server['domain'];
        //execute the API Call
        $result = $client->__call('getLists',$post_fields);
        $result = json_decode($result);
        return $result;
    }

    /**
     * gửi email đến danh sách các email có trên hệ thống cúa sting theo mtype
     * @static
     * @param $fromEmail
     * @param $subject
     * @param $content
     * @param int $mtype
     * @param $start_time
     * @param string $from_name
     * @param string $reply_to
     * @return bool|mixed
     *  example: ("noreply@oka.todo.vn",'gửi mail định kỳ 2','xin chào ta là hahaa',1,'now')
     */
    static function sendMailsBySting($fromEmail,$subject, $content, $mtype=1, $start_time ='now', $from_name='', $reply_to =''){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key'].$fromEmail);
        $post_fields['from_email'] = $fromEmail;
        $post_fields['subject'] = $subject;
        $post_fields['content'] = $content;
        $post_fields['mtype'] = $mtype;
        $post_fields['start_time'] = $start_time;
        $post_fields['from_name'] = $from_name;
        $post_fields['reply_to'] = $reply_to;
        //execute the API Call
        $result = $client->__call('sendMails',$post_fields);
        $result = json_decode($result);
        return $result;
        /*if($result->status == 1){
            return true;
        }else{
            return $result;
        }*/

    }

    /**
     * gửi mail tới một địa chỉ mail khác
     * @static
     * @param $from_email
     * @param $subject
     * @param $content
     * @param $to_email
     * @param string $from_name
     * @param string $cc
     * @param string $bcc
     * @param string $reply_to
     * @return bool|mixed
     */

    static function sendMailToOneBySting($from_email, $subject, $content, $to_email, $from_name='', $cc='', $bcc='', $reply_to=''){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key'].$from_email);
        $post_fields['from_email'] = $from_email;
        $post_fields['subject'] = $subject;
        $post_fields['content'] = $content;
        $post_fields['to_email'] = $to_email;
        $post_fields['from_name'] = $from_name;
        $post_fields['cc'] = $cc;
        $post_fields['bcc'] = $bcc;
        $post_fields['reply_to'] = $reply_to;
        //execute the API Call
        $result = $client->__call('sendMail',$post_fields);
        $result = json_decode($result);
            return $result;
        /*if($result->status == 1){
            return true;
        }else{
            return $result;
        }*/
    }

    /**
     * gửi mail tới nhiều địa chỉ mail khác nhau
     * @static
     * @param $from_email
     * @param $subject
     * @param $content
     * @param $maillist : “email1, email2, ….”
     * @param string $from_name
     * @param string $reply_to
     * @return bool|mixed
     */
    static function sendMailToListBySting($from_email, $subject, $content, $maillist, $from_name='', $reply_to=''){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key'].$from_email);
        $post_fields['from_email'] = $from_email;
        $post_fields['subject'] = $subject;
        $post_fields['content'] = $content;
        $post_fields['maillist'] = $maillist;
        $post_fields['from_name'] = $from_name;
        $post_fields['reply_to'] = $reply_to;
        //execute the API Call
        $result = $client->__call('send_mails',$post_fields);
        $result = json_decode($result);
        if($result->status == 1){
            return true;
        }else{
            return $result;
        }
    }

    /**
     * xem trạng thái send mail của một campain qua hệ thống của sting
     * @static
     * @param $jobId
     * @return bool|mixed
     */
    static function getStatusSendMailSting($jobId){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        //$post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key'].$from_email);
        $post_fields['job_id'] = $jobId;
        //execute the API Call
        $result = $client->__call('getStatus',$post_fields);
        $result = json_decode($result);
        return $result;
        /*if($result->status == 1){
            return true;
        }else{
            return $result;
        }*/
    }

    /**
     * huỷ một campain gửi mail của hệ thống mà mình đã set trước
     * @static
     * @param $jobId
     * @return bool|mixed
     */
    static function stopSendMailSting($jobId){
        $client = new SoapClient("http://" . CGlobal::$mail_sting_server['host'].':'.CGlobal::$mail_sting_server['port'].'/MailService//soap/description');
        $post_fields['secure_key'] = md5(CGlobal::$mail_sting_server['domain'].CGlobal::$mail_sting_server['secret_key'].$jobId);
        $post_fields['domain'] = CGlobal::$mail_sting_server['domain'];
        $post_fields['job_id'] = $jobId;
        //execute the API Call
        $result = $client->__call('stop_job',$post_fields);
        $result = json_decode($result);
        if($result->status == 1){
            return true;
        }else{
            return $result;
        }
    }
}
?>